//
//  MyTopicController.m
//  FbeeAPP
//
//  Created by dev-m on 2018/3/9.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "MyTopicController.h"
#import "TopicObj.h"
#import "BeeGroupTopicCell.h"
#import "LargeTitle.h"
#import "UIScrollView+BCYRefresh.h"

@interface MyTopicController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *listArray;
    UITableView *mainTable;
    UIView *navBar;
    NSUInteger pageNum;
    LargeTitle *header;
}

@end

@implementation MyTopicController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    listArray = [[NSMutableArray alloc] init];
    pageNum = 1;

    self.titleLabel.text = NSLocalizedString(@"tNavUCenterTopic", nil);
    self.largeTitle.hidden = YES;
    [self buildView];
    
    [self bringTitleToFront];
    [self.view bringSubviewToFront:navBar];
    
    [self getListData];
    [LoadingHelper showLoadingOnView:self.view];

}

-(void)buildView
{
    navBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONEWIDTH, NavBarHeight)];
    navBar.backgroundColor = FBEE_COLOR_BACKGROUND;
    [self.view addSubview:navBar];
    
    UIButton *backNav =  [UIButton makeButton:^(TTButtonExtend *make) {
        make.setFrame(CGRectMake(0, StatusHeight, 60, 44));
        make.addImage([UIImage imageNamed:@"nav_back"],UIControlStateNormal);
        make.addTarget(self,@selector(navback));
        make.imageEdgeInsets = UIEdgeInsetsMake(13.5, 16, 13.5, 27);
        make.tag = 666;
        [navBar addSubview:make];
    }];
    
    header = [[LargeTitle alloc] initWithTitle:NSLocalizedString(@"tNavUCenterTopic", nil)];
    
    mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, IPHONEWIDTH, IPHONEHEIGHT) style:UITableViewStyleGrouped];
    mainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    mainTable.alwaysBounceVertical = YES;
    mainTable.backgroundColor = [UIColor clearColor];
    mainTable.tableHeaderView = header;
    mainTable.delegate = self;
    mainTable.dataSource = self;
    [self.view addSubview:mainTable];
    
    __weak id sel = self;
    [mainTable addHeaderByRefreshHandler:^{
        [sel refreshData];
        
    }];
    
    [mainTable addFooterByRefreshHandler:^{
        [sel loadMore];
    }];
    
    [self bringTitleToFront];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma - mark scrollView

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //导航渐变
    if (mainTable.top >= 0) {
        
        float alphaY = scrollView.contentOffset.y;
        
        float alpha = alphaY / (scrollView.bounds.size.height/5.0f);
        alpha = alpha<0?0:alpha;
        alpha = alpha>1?1:alpha;
        [self.navigationView setAlpha:alpha];
        [self.view bringSubviewToFront:self.navigationView];
        
        if (self.navigationView.alpha == 1) {
            navBar.hidden = YES;
        }
        else
        {
            navBar.hidden = NO;
        }
        
    }
}

#pragma mark - TableView Cell

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return listArray.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopicObj *obj = [listArray objectAtIndex:indexPath.row];
    return obj.height;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellId = @"CELLID";
    
    TopicObj *obj = [listArray objectAtIndex:indexPath.row];
    
    BeeGroupTopicCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (!cell) {
        cell = [[BeeGroupTopicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    cell.dataObj = obj;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopicObj *obj = [listArray objectAtIndex:indexPath.row];
    [[JumpManager sharedManager] jumpToTopicDetail:obj];
}

#pragma mark - request

-(void)refreshData
{
    if ([LoadingHelper isLoadingFromView:self.view]) {
        
        [mainTable endRefreshing];
        
        return;
    }
    
    pageNum = 1;
    [self getListData];
}

-(void)loadMore
{
    if ([LoadingHelper isLoadingFromView:self.view]) {
        
        [mainTable endRefreshing];
        
        return;
    }
    
    [self getListData];
}

-(void)getListData
{
    NSString *url = [NSString stringWithFormat:@"%@?uid=%@&pageNum=%@&pageSize=10",MyTopicList_URL,[UserInfoManager sharedManager].userId,@(pageNum)];
    [self get:url code:MyTopicList_URL_Code];
}

#pragma mark - response

-(void)requestSuccess:(id)data code:(NSInteger)code
{
    [LoadingHelper removeLoadingOnView:self.view];
    
    if (code == MyTopicList_URL_Code) {
        
        [mainTable endRefreshing];
        
        [EmptyViewHelper removeEmptyFromView:mainTable];
        
        if (pageNum == 1) {
            [listArray removeAllObjects];
        }
        
        NSArray *dataArray = [data objectForKey:@"topicList"];
        
        if (dataArray.count == 0) {
            
            if (listArray.count == 0) {
                UIView *emptyView = [EmptyViewHelper showEmptyOnView:mainTable imageName:@"common_empty" text:NSLocalizedString(@"textEmptyBeeGroup", nil)];
                emptyView.height = mainTable.height - header.height - 35;
                emptyView.top = 0;
            }
            
        }
        else
        {
            pageNum++;
        }
        
        for (NSDictionary *dataDict in dataArray)
        {
            TopicObj *obj = [[TopicObj alloc] init];
            [obj setObjByDict:dataDict];
            obj.nickname = [UserInfoManager sharedManager].nickName;
            obj.isJoin = YES;
            [listArray addObject:obj];
        }
        
        [mainTable reloadData];
        
    }
}

-(void)requestLogicError:(id)data code:(NSInteger)code
{
    [super requestLogicError:data code:code];
    
    [LoadingHelper removeLoadingOnView:self.view];
}

-(void)requestFailure:(NSError *)error code:(NSInteger)code
{
    [LoadingHelper removeLoadingOnView:self.view];
}



@end
